@section('title', '菜单编辑')
@section('content')
    <div class="layui-form-item">
        <label class="layui-form-label">上级：</label>
        <div class="layui-input-block">
            <select name="category" lay-verify="required">
                <option value=""></option>
                <option value="0" {{empty($menu)||(isset($menu['parent_id'])&&$menu['parent_id']==0)?'selected':''}}>一级菜单</option>
                @if(is_array($menus)&&$menus)
                    @foreach($menus as $menus_child)
                        <option value="{{$menus_child['menu_id']}}" {{(isset($menu['parent_id'])&&$menu['parent_id'] == $menus_child['menu_id']) ? 'selected' : ''}}>{{$menus_child==0?'':'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├─'}}{{$menus_child['name']}}</option>
                    @endforeach
                @endif
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称：</label>
        <div class="layui-input-block">
            <input type="text" value="{{$menu['name'] or ''}}" name="name" required lay-verify="name" placeholder="请输入名称" autocomplete="off" class="layui-input">
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片上传：</label>
        <div class="layui-input-block">
            <input type="file" name="img" class="layui-upload-file">
            <p>
                <img id="imagesurl" src="{{$menu['menu_img'] or '/static/admin/images/zwtp.jpg'}}" width="100px" height="100px">
            </p>
            <input type="hidden" id="img" value="{{$menu['menu_img'] or '/static/admin/images/zwtp.jpg'}}" name="img" required lay-verify="user_name" placeholder="图片" autocomplete="off" class="layui-input">
        </div>
    </div>

@endsection
@section('id',$menu_id)
@section('js')
    <script>

        layui.use(['form','jquery','laypage', 'layer','upload'], function() {
            var form = layui.form(),
                $ = layui.jquery;
            form.render();
            upload = layui.upload;

            //普通图片上传
            layui.upload({
                url: '/content/upload'
                ,success: function(res){
                    if(res.code=='200'){
                        $("#img").val('/uploads/'+res.imgurl);
                        $("#imagesurl").attr('src','/uploads/'+res.imgurl);
                        layer.msg(res.msg, {time: 1000});
                        console.log(res.msg); //上传成功返回值，必须为json格式
                    }else{
                        layer.msg(res.msg, {time: 1000});
                    }
                }
            });
            form.verify({
                name: [/^.{2,12}$/, '菜单名长度2到12位之间'],
                uri: [/^\/(.*)$/, 'URL格式错误'],
            });
            form.on('submit(formDemo)', function(data) {

                $.ajax({
                    url:"{{url('/amenues')}}",
                    data:$('form').serialize(),
                    type:'post',
                    dataType:'json',
                    success:function(res){
                        if(res.status == 1){
                            layer.msg(res.msg,{icon:6});
                            var index = parent.layer.getFrameIndex(window.name);
                            setTimeout('parent.layer.close('+index+')',2000);
                            //parent.layer.close(index);
                        }else{
                            layer.msg(res.msg,{shift: 6,icon:5});
                        }
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg('网络失败', {time: 1000});
                    }
                });
                return false;
            });
        });
    </script>
@endsection
@extends('common.edit')
